<? php
//MY_Controller.php:
	class MY_Controller extends CI_Controller{
		public function __construct(){
			parent::__construct();
			//检测登陆后存入_SESSION中的东西;
			$username = $this->session->userdata('UserName');
			$uid = $this->session->userdata('uid');

			if(!$username || !$uid){
				redirect('admin/LoginPage');//跳转函数
			}
		}
	}


config.php:
	$config['sess_expiration']		= 60;//设置重新登陆时间为60s


//admin.php:
	/**
	 * 判断是否允许登陆
	 */
	function LoginApi(){
		$UserName = $this->input->post('UserName');
		$userData = $data = $this->db->get_where('adminuser',array('UserName'=>$UserName))->result_array();//参数为表名+条件
		$Password = $this->input->post('Password');
		if(!$userData || $userData[0]['Password'] != md5($Password)){
			error('用户名或密码不正确！');
		}
		//登陆成功，更新登陆时间
		$this->db->update('adminuser',array('lastlogin'=>strtotime(date("Y-m-d H:i:s"))),array('Id'=>$userData[0]['Id']));
//var_dump($userData);die;			
		
		$sessionData = array(
			'UserName' => $userData[0]['UserName'],
			'uid' => $userData[0]['Id'],
			'Type' => $userData[0]['Type'],
			'lastlogin'=>strtotime(date("Y-m-d H:i:s"))
			);
		$this->session->set_userdata($sessionData);
 
		success('category/admin','登陆成功');
	}

	/**
	 * 退出登录
	 */
	function logout(){
		$this->session->sess_destroy();
		success('admin/LoginPage','退出成功');
	}